<template>
  <WithDialog
    :visible.sync="dialogVisible"
    title="创建库存调整单"
    :confirm-btn-loading="loading"
    @confirm="handleConfirm"
    @cancel="handleCancel"
  >
    <el-alert
      title="温馨提示"
      type="warning"
      :closable="false"
      style="margin-bottom: 10px;"
    >
      如若需要减少库存，在“调整数量”一栏中输入负数即可，减少的数量不得超过库存总量； 增加库存则反之。
    </el-alert>
    <Form
      ref="formRef"
      :model="model"
      :current-goods-quantity="inventoryQuantity"
    />
  </WithDialog>
</template>
<script>
import WithDialog from '@/components/WithDialog/withDialog.vue'
import Form from '../goodsInventory/Form.vue'
import { cloneDeep } from 'lodash-es'
export default {
  name: '',
  components: {
    WithDialog,
    Form
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },

    goodsTypeOptions: {
      type: Array,
      default: () => []
    },

    loading: {
      type: Boolean,
      default: false
    },

    record: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      model: {
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(value) {
        this.$emit('update:visible', value)
      }
    },

    inventoryQuantity() {
      return this.record?.old_quantity
    }
  },
  watch: {
    dialogVisible: {
      immediate: true,
      handler(n) {
        if (n) {
          const { reason, id, adjust_quantity, goods_type_id, inventory_id } = this.record
          this.model = {
            reason,
            id,
            adjust_quantity,
            goods_type_id,
            inventory_id
          }
        }
      }
    }
  },
  methods: {
    async handleConfirm() {
      const res = await this.$refs.formRef.validate()
      if (!res) return this.$message.error('请将信息填写完整')
      this.$emit('confirm', this.model)
    },

    handleCancel() {
      this.$emit('update:visible', false)
    },

    resetFields() {
      this.$refs.formRef.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep(.el-dialog__body) {
  padding-top: 0;
}
</style>
